Welcome back.
In this lecture.
We're going to be implementing a day time cycle.
Now I've already covered this system in a previous lecture.
But we're going to go through it again once more for this project.
So go ahead and create a new module script underneath our server script service.
That's going to act as a new service.
And we can call this module our Time Cycle service.
Then I'm going to paste in that thing we've been using or that template we've been using, and I'll
rename this to our time cycle service.
Inside of this module, we're going to need the lighting service.
As well as the run service.
And we're also going to be using the tween service.
Now, I also want to go ahead and start playing a sound in the workspace when the time turns to night.
So inside of the workspace I have a folder called Global Sounds and then we have some ambience to play
when it's night time, so I'll make a quick reference to that.
In this script we'll call it night ambience.
That's equal to workspace dot global sounds and we'll get night ambience.
Now we can go ahead and define some constants for our module.
One would be how long we would like the day to last in seconds.
So for example we can set it to 300.
And that means the entire, uh day time cycle would last 300 seconds.
We can set what hour we would like to start the day at, so I'll call it start hour.
And we could set it at something like 9 a.m..
And then we're going to define the max brightness.
So this is going to be the brightness set.
And the lighting service.
When we're in the daytime we could set it to three.
And then we can go ahead and define a minimum brightness.
And this is the brightness we're going to set when it's nighttime.
And then we can calculate a brightness delta which is something we're going to increment the brightness
in the lighting service every single time a second goes by.
So we can set this equal to the max brightness.
And then just divide that by the day length.
So that gives us our delta and then I'm going to create a variable I'm going to call it start.
And this is going to represent the current time or the current tick based on what our start hour is
going to be.
So we're going to set equal to the tick.
And we need to subtract it by our start hour.
We get our start our going to divide it by 24.
And then we're going to multiply that by the day length.
So we're going to get start hour divided by 24 and then multiply it by our day length.
So basically this is going to give us, um, a number here to subtract the current tick by to represent
how many seconds we have progressed so far in the day.
If we have set it at 9 a.m., then that means this value here is going to represent how many seconds
has elapsed for it to be 9 a.m..
So nine divided by 24 is that fraction or the percentage of how long we progressed through the day.
And then we just multiply that by the day length to get that number of seconds.
And then we subtract it to the current tick.
So that way we get our start time.
And then we can go ahead and define a private function.
And I'm going to call this on heartbeat because we're going to connect to the heartbeat event on the
run service to constantly update the time that has been elapsed and update the hour of day and the clock
time.
So in our start function, that's where we'll go ahead and refer to the heartbeat event and connect
our own heartbeat function to it.
And then in the initialize function what we're going to do is we're going to listen for when the clock
time property in the lighting service changes.
So get property change signal clock time.
We'll connect and get a function.
And this is where we can check what time it is, if it's daytime or if it's nighttime.
If it's daytime, then we can stop the night ambience from playing.
But if it's nighttime, then we can start playing the night ambience and we can go ahead and define
daytime and night time.
So for example, we could create a couple more constants.
One we could define as day time start and we can set it to like maybe, I don't know, 6 a.m. and then
we could go ahead and define another one and this will be our night time start.
And actually let me go ahead and add a couple extra underscores.
And then for the night time we could set it to maybe 18 which would be about 6:00.
And then we could go ahead and check if the clock time.
So if lighting service clock time is greater than our night time start.
Or if our lighting service clock time is less than our day start.
Then that means it's night time and we should start playing the night ambiance.
So what we can do is we can refer to our night ambiance and start playing it.
But I also want to like, fade it in.
So we're going to set the night ambiance volume equal to zero.
And then we can fade it in using the tween service.
So we'll do tween service create on night ambience.
We'll do a tween info dot new.
And the duration we want to fade.
The night ambience in is going to depend on our day length.
So what I'll do is I'll create a variable, I'll call it tween duration, and it's going to be equal
to the day length.
And then we can just like multiply it by 0.5.
So we'll cut it in half and then we'll multiply this value by another fraction I don't know something
like 0.05.
So for example if the day length is 300 seconds, what we could do is we could print out like 300 seconds
times 0.5 and then multiply that by 0.05.
So that means the total tween would last 7.5 seconds if the day length was like.
Uh, let's say we want to set it to 1000 seconds, and then we enter that in.
Then the tween, the total tween would last about 25 seconds.
So it really depends on the day length.
And then we'll just pass that tween duration here.
And we'll just adjust the volume of our night ambience to be equal.
Two.
We could do a value of like 0.5 seconds.
Now we also want to make sure that we don't play the night ambience again if it's already playing.
So if our night ambience is already playing, so is playing.
Then we're just going to return.
And then.
If the night ambience is playing right and it's currently nighttime.
So what we want to do is we want to make sure the ambience is plain, and then we want to make sure
the night ambience volume is equal to 0.5.
This means we haven't tween the volume to fade out yet.
So what we can do again is just copy this, paste this here and this time we want to tween the volume
to be zero.
And then instead of playing the tween directly, we'll get that tween in a variable.
And then we'll play that tween.
And then once that tween has completed.
Then we can go ahead and stop the night ambience.
And then the other thing we want to do for the initialize function is we want to go ahead and update
the clock time property in the lighting service to be equal to the start hour.
So lighting service clock time is going to be equal to start hour.
And then we also want to update the uh brightness property.
And that's going to be equal to.
And we're going to check if the start hour is greater than the day time start.
And we want to make sure that the start hour is less than the night time start.
And if it is, that means it's the daytime.
So we can set the brightness equal to the max brightness, else we'll set it to the minimum brightness.
Okay, so now we can go ahead and fill out our on heartbeat function.
So inside of here, what we want to do is we want to check the current time that has elapsed, and that's
going to be equal to the current tick minus our start time.
So as tick continues to update, we're slowly moving forward in time.
And that will allow us to calculate the hour of day because we're getting how many seconds has elapsed
so far.
And then we'll compare that value to our day length.
So we can go ahead and create a variable.
I'll call it hour of day.
And that's going to be equal to we're going to get our current time elapsed and divide that by our day
length.
And then we can go ahead and multiply that value by how many hours are in a day.
So that would be 24.
And this would give us the current hour of day.
That means we're able to set the lighting server stock clock time equal to this hour.
Now of course, we also want to check, um, if the current hour of day is greater than our day time
start and our hour of day is less than the night time start.
If it is, then we want to signify that the brightness should be set to max brightness.
Otherwise the brightness should be set to the minimum brightness.
So we can create a variable.
I'll call it target brightness.
And then we can set target brightness here equal to max brightness.
Otherwise here we'll set target brightness equal to the minimum brightness.
And then we can go ahead and check if the lighting service dot brightness is less than our target brightness.
If that is the case, then we need to go ahead and increment lighting service dot brightness.
That's going to be equal to math dot min, because we need to make sure it doesn't go beyond our max
brightness.
So we'll pass target brightness here.
That's going to be the minimum number.
And then we're going to pass lighting service dot brightness plus our brightness delta.
Otherwise, if it's nighttime and we need to decrease the brightness.
So if the lighting service dot brightness is greater than our target brightness, then what we need
to do is exactly the same thing that we did up here.
But this time we need to set the brightness equal to math dot max.
Which would be zero.
And then we're going to decrement by the brightness delta.
And then another important thing we need to check is if the time that has elapsed so far is greater
than or equal to our day length.
If it is, then we need to update our start value and our start value is going to be updated to the
current tick.
So now we have this time cycle service setup.
And if we go and playtest our game.
And we go ahead and look at our lighting service.
As you can see, the clock time, uh, value is slowly incrementing.
And if we go ahead and take a look at the sun, as you can see, our sun is slowly moving and the entire
24 hour cycle is going to take 300 seconds.
Now, of course, we can also go in here and adjust this to be less.
So maybe we only want the day to last a total of 60s.
So we'll go and play test the game.
And we can see the lighting service clock time is incrementing much faster.
And if we look at the sun, as you can see, there it goes.
And then we'll make sure once it hits nighttime that it'll start playing the night ambience.
So here we go.
There we go.
Our night ambience faded in.
There's the moon.
And then we'll go ahead and see if our night ambience fades out as well.
As you can see, our brightness has now increased and now we're back at the daytime and the night ambiance
has faded out.
Very cool.
Now there is one more thing we need to add, which is updating the collision group for all of the players
in our game.
See you in the next lecture.